package org.spring.bse.home.controller;

import java.util.List;

import javax.xml.bind.JAXBException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.spring.bse.home.bo.BseIndiaBO;
import org.spring.bse.home.model.Stock;
import org.spring.bse.home.model.Wrapper;
import org.spring.bse.home.util.JSONHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

@Controller
@RequestMapping("bseIndia")
//
public class BseIndiaController
{
	BseIndiaBO bseIndiaBO = new BseIndiaBO();

	@RequestMapping("/homePage")
	public ModelAndView homePage()
	{
		System.out.println("homePage::---------------INTO");
		return new ModelAndView("hello", "msgStr", "");
	}
	
	@RequestMapping("/persistGoogleXMLFeed")
	public ModelAndView persistGoogleXMLFeed(@RequestParam String scripCSV)
	{
		System.out.println("persistGoogleXMLFeed::Start");
		String MESSAGE = null;
		if (scripCSV != null && !scripCSV.trim().isEmpty())
		{
			String queryStr = "stock=" + scripCSV.replaceAll(",", "&stock=");
			MESSAGE = bseIndiaBO.persistGoogleXMLFeed(queryStr);
		}
		System.out.println("persistGoogleXMLFeed::---------------END");
		return new ModelAndView("hello", "msgStr", MESSAGE);
	}

	@RequestMapping("/scripCodes")
	public ModelAndView getActiveScripCodes() 
	{
		String MESSAGE = null;

		MESSAGE = bseIndiaBO.getActiveScripCodes();

		return new ModelAndView("hello", "msgStr", MESSAGE);
	}
	
	@RequestMapping("/getAllStocks")
	public ModelAndView getAllStocks() throws JAXBException
	{
		String MESSAGE = null;
		System.out.println("getAllStocks::---------------START");
		
		List<Stock> stockList = bseIndiaBO.getAllStocks();
		System.out.println("stockList.size::"+stockList.size());
		Wrapper wrap = new Wrapper(stockList);
		
		Gson gson = new Gson();
		MESSAGE = gson.toJson(wrap);
		System.out.println("getAllStocks::---------------END");
		return new ModelAndView("message", "msgStr", MESSAGE);
	}

	@RequestMapping("/stockCurPrice/{scripCode}")
	public ModelAndView stockCurPrice(@PathVariable("scripCode") String scripCode)
	{

		if (scripCode.equals("null"))
		{
			return new ModelAndView("hello", "msgStr", "");
		}

		String strUrl = "https://www.google.com/finance?q=BOM:" + scripCode;
		String MESSAGE = null;
		Stock stock = new Stock();
		Document doc;
		try
		{
			doc = Jsoup.connect(strUrl).get();
			stock.setScripName(doc.select("#companyheader > div > h3").get(0).text().trim());
//			stock.setCurValue(doc.select("#ref_677432_l").get(0).text().trim());
//			stock.setChgValue(doc.select("#ref_677432_c").get(0).text().trim());
//			stock.setChgPer(doc.select("#ref_677432_cp").get(0).text().trim().replace("(", "").replace(")", ""));

			String updatedAt = doc.select("#price-panel span.nwp").get(0).text().trim();
			updatedAt = updatedAt.contains("Close") ? "Close" : updatedAt.split(" ")[2].trim();
			stock.setUpdatedAt(updatedAt);

			stock.setScripID(doc.select("input.fjfe-searchbox-input").get(0).val().trim().split(":")[1]);

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		MESSAGE = JSONHelper.prepareStockObject(stock);
		return new ModelAndView("hello", "msgStr", MESSAGE);
	}

	@RequestMapping("/updateStocks")
	public ModelAndView updateStocks(@PathVariable("scripCode") String scripCode)
	{

		return new ModelAndView("hello", "msgStr", "");
	}

	@RequestMapping(method=RequestMethod.PUT, consumes="text/plain", value = "/queryStocks")
	public ModelAndView queryStocks()
	{
		String MESSAGE = null;
		System.out.println("queryStocks::---------------START");
		
		List<Stock> stockList = bseIndiaBO.getAllStocks().subList(1, 200);
		System.out.println("stockList.size::"+stockList.size());
		Wrapper wrap = new Wrapper(stockList);
		
		Gson gson = new Gson();
		MESSAGE = gson.toJson(wrap);
		System.out.println("queryStocks::---------------END");

		return new ModelAndView("hello", "msgStr", MESSAGE);
	}
	
}

// String url = "https://www.google.com/finance/historical?q=BOM%3A" + scripCode
// + "&ei=_P-XUbiHL4GglgP1DA&start=0&num=100";
// http://www.bseindia.com/corporates/List_Scrips.aspx?expandable=1
// String url = "http://www.google.com/ig/api?stock=500295";

//=CONCATENATE("INSERT INTO [stocks] ([scripCode],[scripID],[scripName],[bseStatus],[bseGroup],[faceValue],[ISIN],[industry]) VALUES(",A2,",'",B2,"','",C2,"','",D2,"','",TRIM(E2),"',",F2,",'",G2,"','",H2,"');")